package com.example.collections;

import java.util.*;

/**
 * 集合框架示例
 * 对比C#的集合类型
 */
public class CollectionExamples {
    
    public static void main(String[] args) {
        CollectionExamples examples = new CollectionExamples();
        
        System.out.println("=== Java集合框架示例 ===");
        
        // List示例
        examples.listExample();
        
        // Set示例
        examples.setExample();
        
        // Map示例
        examples.mapExample();
        
        // 集合操作示例
        examples.collectionOperations();
    }
    
    /**
     * List示例 (类似C#的List<T>)
     */
    public void listExample() {
        System.out.println("\n--- List示例 ---");
        
        // ArrayList (动态数组，类似C#的List<T>)
        List<String> names = new ArrayList<>();
        names.add("张三");
        names.add("李四");
        names.add("王五");
        names.add("赵六");
        
        System.out.println("原始列表: " + names);
        System.out.println("列表大小: " + names.size());
        System.out.println("第一个元素: " + names.get(0));
        
        // 遍历List
        System.out.println("遍历列表:");
        for (String name : names) {
            System.out.print(name + " ");
        }
        System.out.println();
        
        // 使用索引遍历
        System.out.println("使用索引遍历:");
        for (int i = 0; i < names.size(); i++) {
            System.out.print(names.get(i) + " ");
        }
        System.out.println();
        
        // 使用Stream API (Java 8+)
        System.out.println("使用Stream API:");
        names.stream().forEach(name -> System.out.print(name + " "));
        System.out.println();
        
        // LinkedList (链表)
        List<Integer> numbers = new LinkedList<>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        numbers.addFirst(0); // 在开头添加
        numbers.addLast(4);  // 在结尾添加
        
        System.out.println("LinkedList: " + numbers);
    }
    
    /**
     * Set示例 (类似C#的HashSet<T>)
     */
    public void setExample() {
        System.out.println("\n--- Set示例 ---");
        
        // HashSet (无序，不重复)
        Set<String> uniqueNames = new HashSet<>();
        uniqueNames.add("张三");
        uniqueNames.add("李四");
        uniqueNames.add("张三"); // 重复元素会被忽略
        uniqueNames.add("王五");
        
        System.out.println("HashSet: " + uniqueNames);
        System.out.println("包含'张三': " + uniqueNames.contains("张三"));
        
        // TreeSet (有序，不重复)
        Set<Integer> sortedNumbers = new TreeSet<>();
        sortedNumbers.add(3);
        sortedNumbers.add(1);
        sortedNumbers.add(4);
        sortedNumbers.add(2);
        
        System.out.println("TreeSet (自动排序): " + sortedNumbers);
        
        // LinkedHashSet (保持插入顺序)
        Set<String> orderedNames = new LinkedHashSet<>();
        orderedNames.add("王五");
        orderedNames.add("张三");
        orderedNames.add("李四");
        
        System.out.println("LinkedHashSet (保持插入顺序): " + orderedNames);
    }
    
    /**
     * Map示例 (类似C#的Dictionary<K,V>)
     */
    public void mapExample() {
        System.out.println("\n--- Map示例 ---");
        
        // HashMap (无序)
        Map<String, Integer> ages = new HashMap<>();
        ages.put("张三", 25);
        ages.put("李四", 30);
        ages.put("王五", 28);
        ages.put("赵六", 35);
        
        System.out.println("HashMap: " + ages);
        System.out.println("张三的年龄: " + ages.get("张三"));
        System.out.println("包含'张三': " + ages.containsKey("张三"));
        
        // 遍历Map
        System.out.println("遍历Map:");
        for (Map.Entry<String, Integer> entry : ages.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue() + "岁");
        }
        
        // 只遍历键
        System.out.println("所有姓名:");
        for (String name : ages.keySet()) {
            System.out.print(name + " ");
        }
        System.out.println();
        
        // 只遍历值
        System.out.println("所有年龄:");
        for (Integer age : ages.values()) {
            System.out.print(age + " ");
        }
        System.out.println();
        
        // TreeMap (按键排序)
        Map<String, Integer> sortedAges = new TreeMap<>(ages);
        System.out.println("TreeMap (按键排序): " + sortedAges);
        
        // LinkedHashMap (保持插入顺序)
        Map<String, Integer> orderedAges = new LinkedHashMap<>();
        orderedAges.put("王五", 28);
        orderedAges.put("张三", 25);
        orderedAges.put("李四", 30);
        
        System.out.println("LinkedHashMap (保持插入顺序): " + orderedAges);
    }
    
    /**
     * 集合操作示例
     */
    public void collectionOperations() {
        System.out.println("\n--- 集合操作示例 ---");
        
        // 创建两个列表
        List<String> list1 = Arrays.asList("张三", "李四", "王五");
        List<String> list2 = Arrays.asList("王五", "赵六", "钱七");
        
        System.out.println("列表1: " + list1);
        System.out.println("列表2: " + list2);
        
        // 并集
        Set<String> union = new HashSet<>(list1);
        union.addAll(list2);
        System.out.println("并集: " + union);
        
        // 交集
        Set<String> intersection = new HashSet<>(list1);
        intersection.retainAll(list2);
        System.out.println("交集: " + intersection);
        
        // 差集
        Set<String> difference = new HashSet<>(list1);
        difference.removeAll(list2);
        System.out.println("差集 (list1 - list2): " + difference);
        
        // 集合转换
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        System.out.println("原始数字: " + numbers);
        
        // 过滤 (类似C#的Where)
        List<Integer> evenNumbers = numbers.stream()
                .filter(n -> n % 2 == 0)
                .toList();
        System.out.println("偶数: " + evenNumbers);
        
        // 映射 (类似C#的Select)
        List<String> numberStrings = numbers.stream()
                .map(Object::toString)
                .toList();
        System.out.println("转换为字符串: " + numberStrings);
        
        // 排序
        List<String> names = Arrays.asList("王五", "张三", "李四", "赵六");
        List<String> sortedNames = names.stream()
                .sorted()
                .toList();
        System.out.println("排序后: " + sortedNames);
    }
} 